home *** CD-ROM | disk | FTP | other *** search
/ Programmers Heaven 2 / Programmers Heaven 2.iso / files / graphics / library / wgt51_r2.zip / WGT5 / SPR5 / CONVERT / SPR2BGI.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-05-27  |  7.5 KB  |  283 lines

  1. uses Crt, Graph,Strings;
  2.  
  3. type
  4.   sprite_array =  array [0..2000] of pointer;
  5. var
  6.   Gd, Gm: Integer;
  7.  
  8.   sprites: sprite_array;
  9.   blocksize:array [0..2000] of longint;
  10.  
  11. {
  12. Generic sprite conversion utility
  13.  
  14. This version uses the BGI to load a sprite file, and convert it into
  15. 640x350x16 mode.  Note that you cannot create sprites with more than
  16. 16 colors and expect it to look right after converting it.  To do this
  17. correctly, draw using the first 16 colors only.  If you modify the
  18. palette, you will need to chnage it yourself using the first 16 colors
  19. in the palette variable.
  20.  
  21. This program can be used as a generic conversion program.   Simply replace
  22. the BGI graphics commands with any similar commands from another graphics
  23. library.
  24.  
  25. The main commands you will need to change are:
  26.     initialization routines
  27.     putpixel
  28.     imagesize   (calculates the number of bytes required in an image)
  29.     farmalloc   (WGT calculates the size and allocates the memory for you,
  30.         other libraries may not.   You will have to be flexible
  31.         here.)
  32.     getimage
  33.     putimage
  34.     getpalette
  35.     setrgbpalette
  36.     cleardevice
  37.     bar
  38.  
  39.  
  40.  Note that the Pascal version is much slower than the C version since
  41.  I used blockread.
  42.  }
  43.  
  44.  function FileExists(FileName: String): Boolean;
  45. { Boolean function that returns True if the file exists;otherwise,
  46.    it returns False. Closes the file if it exists. }
  47. var
  48.   F: File;
  49. begin
  50.   {$I-}
  51.   Assign(F, FileName);
  52.   Reset(F);
  53.   Close(F);
  54.   {$I+}
  55.   FileExists := (IOResult = 0) and (FileName <> '');
  56.  end;  { FileExists }
  57.  
  58. procedure convertsprites(infilename,outfilename:string);
  59. var
  60. filein:Text; { 256 color sprite file }
  61. fileout:File; { converted sprite file }
  62. palette:array [1..768] of char; { 256 * 3 (RGB values) }
  63. size: longint;
  64. maxcolor,maxsprite:integer;
  65. temp:pointer;
  66. a,b,i,j,spritemade:integer;
  67. buf: array [1..13] of char;  { Used for header }
  68. x,y:integer; { Screen location }
  69. col:char;  { Pixel color }
  70. c1,c2:char;
  71. result:integer;
  72. startingsprite:integer;
  73.  
  74. begin
  75.  
  76.  
  77.     { Open the files }
  78. if FileExists(infilename) then
  79.    begin
  80.     Assign(filein,infilename);
  81.     Reset(filein);
  82.     Assign(fileout,outfilename);
  83.     Rewrite(fileout,1);
  84.  
  85.     Read(filein,c1);
  86.     Read(filein,c2);
  87.     a:=256*ord(c2)+ord(c1);
  88.      { Get the version number, and change the startingsprite accordingly.
  89.      If version <= 3, maxsprite contains the maximum number of sprites
  90.      that can be stored in a file.  If version > 4, maxsprite contains
  91.      the number of the highest sprite in the file. (empty sprites at
  92.      the end are not kept in the file. }
  93.     if (a <= 3) then
  94.       startingsprite := 1
  95.     else startingsprite := 0;    { Version 4 starts at sprite 0 }
  96.  
  97.     for i:=1 to 13 do
  98.     read(filein,buf[i]); { sprite header }
  99.  
  100.     if (buf=' Sprite File ') then { see if it is a sprite file }
  101.        begin
  102.         for i:=1 to 768 do
  103.          read(filein,palette[i]); { Read in 256 color palette }
  104.     maxcolor:=getmaxcolor+1;
  105.     blockwrite(fileout,maxcolor,2); { Write the number of colors stored in file }
  106.  
  107.         blockwrite(fileout,palette,maxcolor*3);
  108.         { write palette }
  109.  
  110.         Read(filein,c1);
  111.         Read(filein,c2);
  112.         maxsprite:=256*ord(c2)+ord(c1);  { maximum sprites in this file }
  113.     blockwrite(fileout,maxsprite,2);
  114.     for i:= startingsprite to maxsprite do
  115.            begin { load them in }
  116.             Read(filein,c1);
  117.             Read(filein,c2);
  118.             spritemade:=256*ord(c2)+ord(c1); { flag to see if sprite exists }
  119.         blockwrite(fileout,spritemade,2);
  120.         if (spritemade = 1) then
  121.            begin
  122.         SetFillStyle(SolidFill,0);
  123.         bar(0,0,319,199);  { maximum sprite size }
  124.                 Read(filein,c1);
  125.                 Read(filein,c2);
  126.                 a:=256*ord(c2)+ord(c1); { width }
  127.                 Read(filein,c1);
  128.                 Read(filein,c2);
  129.                 b:=256*ord(c2)+ord(c1); { height }
  130.         blockwrite(fileout,a,2); { put width and height }
  131.         blockwrite(fileout,b,2);
  132.  
  133.         { Read in the image data. Each byte represents a color
  134.         from 0-255. Obviously converting sprites that use more colors
  135.         than the current mode allows will not work. Draw sprites using
  136.         only the first colors (eg 0-16), up to maxcolors of the mode you're using. }
  137.         for y:=0 to b-1 do
  138.           for x:=0 to a-1 do
  139.                      begin
  140.                       read(filein,col);
  141.               putpixel(x,y,ord(col));
  142.                      end;
  143.  
  144.         size := imagesize(0, 0, a-1, b-1); { get byte size of image }
  145.         getmem(temp,size);
  146.                 if (temp = nil) then
  147.              begin
  148.                       closegraph;
  149.               writeln('Error: not enough heap space in convertsprites.');
  150.               halt(1);
  151.              end;
  152.         getimage(0, 0, a-1, b-1,temp^); { Get the image in new mode }
  153.         blockwrite(fileout,temp^,size); { Write the data in getimage format }
  154.             freemem(temp,size);
  155.            end;
  156.            end;
  157.            Close(filein);
  158.            Close(fileout);
  159.        end;
  160.     end
  161.   else
  162.      begin
  163.        closegraph;
  164.        writeln('Could not open 256 color sprite file');
  165.        halt(1);
  166.      end;
  167. end;
  168.  
  169. procedure freesprites(freespr:sprite_array);
  170. var
  171. i:integer;
  172. width,height:integer;
  173. begin
  174.  
  175. for i:=0 to 2000 do
  176.   begin
  177.   if freespr[i] <> nil then
  178.     freemem(freespr[i],blocksize[i]);
  179.   end;
  180. end;
  181.  
  182.  
  183.  
  184. procedure loadsprites(infilename:string;var loadspr:sprite_array);
  185. var
  186. filein:file;   { converted color sprite file }
  187. maxcolor,maxsprite:integer;
  188. size:longint;
  189. temp: pointer;
  190. a,b,i,j,spritemade:integer;
  191. red,green,blue:char;
  192. buf:Pchar;
  193. pal:palettetype;
  194. begin
  195.     { Open the files }
  196.     if FileExists(infilename) then
  197.      begin
  198.       Assign(filein,infilename);
  199.       Reset(filein,1);
  200.       blockread(filein,maxcolor,2);
  201.  
  202.       getpalette(pal);
  203.       for i:=0 to maxcolor-1 do
  204.         begin
  205.           blockread(filein,red,1);
  206.           blockread(filein,green,1);
  207.           blockread(filein,blue,1);
  208.           setrgbpalette(pal.colors[i],ord(red),ord(green),ord(blue));
  209.       end;
  210.  
  211.       blockread(filein,maxsprite,2);   { maximum sprites in this file }
  212.  
  213.     for i := 0 to maxsprite-1 do { load them in }
  214.        begin
  215.         blockread(filein,spritemade,2); { flag to see if sprite exists }
  216.         if (spritemade = 1) then
  217.            begin
  218.         blockread(filein,a,2); { get width and height }
  219.         blockread(filein,b,2);
  220.  
  221.         size := imagesize(0, 0, a-1, b-1); { get byte size of image }
  222.                 blocksize[i]:=size;
  223.                 getmem(loadspr[i],size);
  224.         if (loadspr[i] = nil) then
  225.             begin
  226.               closegraph;
  227.               writeln('Error: not enough heap space in loadsprites().');
  228.               freesprites(loadspr);
  229.               halt(1);
  230.            end;
  231.                 blockread(filein,loadspr[i]^,size);
  232.            end
  233.          else loadspr[i]:=nil;
  234.  
  235.          end;
  236.     close(filein);
  237.   end;
  238. end;
  239.  
  240.  
  241.  
  242. procedure testsprites;
  243. { Loops through 10 sprites, displaying them on the screen
  244.  using the putimage method. Press a key to go to the next sprite. }
  245. var
  246. i,j:integer;
  247. begin
  248.  
  249. for i:=0 to 10 do
  250.   begin
  251.   cleardevice;
  252.  
  253.    if sprites[i] <> nil then
  254.      begin
  255.      for j:=1 to 20 do
  256.        putimage(random(getmaxx),random(getmaxy),sprites[i]^,NormalPut);
  257.        { Put the converted image on the screen }
  258.      while not keypressed do;
  259.      readkey;
  260.      end;
  261.   end;
  262. end;
  263.  
  264. begin
  265.   Gd:= VGA;
  266.   Gm:= VGAMED;
  267.  
  268.   InitGraph(Gd, Gm, '');
  269.   if GraphResult <> grOk then
  270.     Halt(1);
  271.  
  272.  
  273.    convertsprites('sprtconv.spr','out.spr');
  274.    loadsprites('out.spr',sprites);
  275.    testsprites;
  276.    freesprites(sprites);
  277.  
  278.  
  279.    { clean up }
  280.    CloseGraph;
  281. end.
  282.  
  283.